package com.ipcoder;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.Socket;
import java.util.Scanner;

//TCP模式下的数据传送
public class TcpSocket {
    public static void main(String[] args) throws IOException {
        while (true) {
            //1.创建socket套接字,输入接收方的IP地址,和共同的端口号
            Socket sk = new Socket("192.168.18.24", 9999);
            //2.通过套接字获得输出流
            OutputStream os = sk.getOutputStream();
            Scanner sc = new Scanner(System.in);
            //输入您要传送的数据
            System.out.println("请输入你要发送的数据:");
            os.write(sc.nextLine().getBytes());
            //关闭流,关闭套接字
            os.close();
            sk.close();
            //Send();
        }
    }

    public static void Send() throws IOException {
        while (true) {
            Socket sk = new Socket("192.168.18.24", 10000);
            OutputStream output = sk.getOutputStream();
            System.out.println("这是TCP模式请输入发送的信息:");
            Scanner sc = new Scanner(System.in);
            output.write(sc.nextLine().getBytes());
            sk.close();
        }
    }
}

class Teat {
    public static void main(String[] args) throws IOException {
        while (true) {
            //建立发送端的套接字接收方的IP地址,和通信的端口号
            Socket sk = new Socket("192.168.18.24", 9999);
            //通过套接字获得输出流
            OutputStream output = sk.getOutputStream();
            System.out.println("这是TCP模式请输入发送的信息:");
            Scanner sc = new Scanner(System.in);
            //向输出流写入数据,只能写入字节,字符串是不能直接写入的
            output.write(sc.nextLine().getBytes());
            //关闭套接字
            sk.close();
        }
    }
}